home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module10- qtzoo / completed source / util.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  9.0 KB  |  245 lines

  1. import java.awt.Color;
  2. import java.awt.Image;
  3. import java.awt.Component;
  4. import java.awt.MediaTracker;
  5. import java.awt.Button;
  6. import java.awt.Toolkit;
  7. import java.awt.event.KeyEvent;
  8. import java.io.*;
  9. import java.net.URL;
  10. import java.net.MalformedURLException;
  11.  
  12. /* Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  13.  *    
  14.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  15.  *                ("Apple") in consideration of your agreement to the following terms, and your
  16.  *                use, installation, modification or redistribution of this Apple software
  17.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  18.  *                please do not use, install, modify or redistribute this Apple software.
  19.  *
  20.  *                In consideration of your agreement to abide by the following terms, and subject
  21.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  22.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  23.  *                reproduce, modify and redistribute the Apple Software, with or without
  24.  *                modifications, in source and/or binary forms; provided that if you redistribute
  25.  *                the Apple Software in its entirety and without modifications, you must retain
  26.  *                this notice and the following text and disclaimers in all such redistributions of
  27.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  28.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  29.  *                Apple Software without specific prior written permission from Apple.  Except as
  30.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  31.  *                are granted by Apple herein, including but not limited to any patent rights that
  32.  *                may be infringed by your derivative works or by other works in which the Apple
  33.  *                Software may be incorporated.
  34.  *
  35.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  36.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  37.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  39.  *                COMBINATION WITH YOUR PRODUCTS.
  40.  *
  41.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  42.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  43.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  45.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  46.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  47.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  */ 
  49.  
  50.  
  51. public class Util
  52. {
  53.     public static final Color GSBColor  = Color.black;
  54.     public static final Color GSWColor  = Color.white;
  55.     public static final Color GS1Color  = new Color(238, 238, 238);
  56.     public static final Color GS2Color  = new Color(221, 221, 221);
  57.     public static final Color GS3Color  = new Color(204, 204, 204);
  58.     public static final Color GS4Color  = new Color(187, 187, 187);
  59.     public static final Color GS5Color  = new Color(170, 170, 170);
  60.     public static final Color GS6Color  = new Color(153, 153, 153);
  61.     public static final Color GS7Color  = new Color(136, 136, 136);
  62.     public static final Color GS8Color  = new Color(119, 119, 119);
  63.     public static final Color GS9Color  = new Color(102, 102, 102);
  64.     public static final Color GS10Color = new Color( 85,  85,  85);
  65.     public static final Color GS11Color = new Color( 68,  68,  68);
  66.     public static final Color GS12Color = new Color( 34,  34,  34);
  67.     public static final Color GSA1Color = new Color( 51,  51,  51);
  68.     public static final Color GSA2Color = new Color( 17,  17,  17);
  69.  
  70.     /**
  71.      * Completely loads the Image referenced by the given filename.
  72.      * This will block until the image is loaded.
  73.      * @param filename the path of the Image to load.
  74.      * @param watcher the component to use to load the image.
  75.      * @param if true, the image location is treated as a resource relative to the
  76.      * watcher component's class file; if false the location is treated as an absolute file path.
  77.      * @return the loaded Image, or null if the loading fails.
  78.      */
  79.     public static Image loadImage(String filename, Component watcher, boolean isResource)
  80.     {
  81.         Image image = null;
  82.         
  83.         if (filename != null)
  84.         {
  85.             URL url = null;
  86.             
  87.             if (isResource)
  88.                 url = watcher.getClass().getResource(filename);
  89.             else
  90.             {
  91.                 try
  92.                 {
  93.                     url = new URL("file", "", filename);
  94.                 }
  95.                 catch(MalformedURLException exc)
  96.                 {
  97.                     exc.printStackTrace();
  98.                 }
  99.             }
  100.             
  101.             if (url == null)
  102.             {
  103.                 System.err.println("loadImage() could not find \"" + filename + "\"");
  104.             }
  105.             else
  106.             {
  107.                 image = watcher.getToolkit().getImage(url);
  108.                 if (image == null)
  109.                 {
  110.                     System.err.println("loadImage() getImage() failed for \"" + filename + "\"");
  111.                 }
  112.                 else
  113.                 {
  114.                     MediaTracker tracker = new MediaTracker(watcher);
  115.         
  116.                     try
  117.                     {
  118.                         tracker.addImage(image, 0);
  119.                         tracker.waitForID(0);
  120.                     }
  121.                     catch (InterruptedException e) { System.err.println("loadImage(): " + e); }
  122.                     finally
  123.                     {
  124.                         boolean isError = tracker.isErrorAny();
  125.                         if (isError)
  126.                         {
  127.                             System.err.println("loadImage() failed to load \"" + filename + "\"");
  128.                             int flags = tracker.statusAll(true);
  129.         
  130.                             boolean loading = 0 != (flags & MediaTracker.LOADING);
  131.                             boolean aborted = 0 != (flags & MediaTracker.ABORTED);
  132.                             boolean errored = 0 != (flags & MediaTracker.ERRORED);
  133.                             boolean complete = 0 != (flags & MediaTracker.COMPLETE);
  134.                             System.err.println("loading: " + loading);
  135.                             System.err.println("aborted: " + aborted);
  136.                             System.err.println("errored: " + errored);
  137.                             System.err.println("complete: " + complete);
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.         
  144.         return image;
  145.     }
  146.  
  147.     /**
  148.      * Reads in a text file to a String.
  149.      * There is probably a more elegant solution, but this works.
  150.      * @param file, the local path to the text file to read.
  151.      * @param localRef, a class file which the text file is local to.
  152.      * @return the string containing the contents of the file.
  153.      * @throws java.io.FileNotFoundException if the given file can not
  154.      * be found to be read.
  155.      */
  156.     public static String readTextResourceFile(String filePath, Object localRef) throws FileNotFoundException
  157.     {
  158.         String result = "";
  159.         
  160.         if (filePath != null && localRef != null)
  161.         {
  162.             InputStream inStream = localRef.getClass().getResourceAsStream(filePath);
  163.             if (inStream == null)
  164.                 throw new FileNotFoundException("Could not find \"" + filePath + "\"");
  165.                 
  166.             //Create a new buffered input stream out of the raw input stream, with a buffer of 8k.
  167.             BufferedInputStream in = new BufferedInputStream(inStream, 8192);
  168.             try
  169.             {
  170.                 StringBuffer sb = new StringBuffer();
  171.                 
  172.                 int c = in.read();
  173.                 while (c != -1)
  174.                 {
  175.                     sb = sb.append((char)c);
  176.                     c = in.read();
  177.                 }
  178.                 
  179.                 result = new String(sb);
  180.             }
  181.             catch (IOException exc)
  182.             {
  183.                 exc.printStackTrace();
  184.             }
  185.         }
  186.         return result;
  187.     }
  188.     
  189.     /**
  190.      * Replaces a given substring with the supplied string in the source string.
  191.      * @param what is the substring of the source string to replace.
  192.      * @param with is the string to use instead.
  193.      * @param source is the original string.
  194.      * @return the modified string with all occurances of 'what' replaced by 'with'
  195.      */
  196.     public static String replace(String what, String with, String source)
  197.     {
  198.         String result = new String(source);
  199.         
  200.         int index = source.indexOf(what);
  201.         while (index >= 0)
  202.         {
  203.             result = (result.substring(0, index) + with + result.substring(index + what.length()));
  204.             index = result.indexOf(what);
  205.         }
  206.         
  207.         return result;
  208.     }
  209.     
  210.     /**
  211.      * A function to simulate a click on the target button.
  212.      * This will make the button draw as if it had been pressed and
  213.      * released, and the button will fire an Action event as if
  214.      * the button were pressed.
  215.      * For use with the Apple MRJ 2.1 EA3 and later.
  216.      */
  217.     public static void simulateClick(Button target)
  218.     {
  219.         if (target != null)
  220.         {
  221.             KeyEvent keyEvent = new KeyEvent(target, KeyEvent.KEY_PRESSED, 
  222.                                            System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, 
  223.                                            (char)KeyEvent.VK_ENTER);
  224.             target.dispatchEvent(keyEvent);
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * Calculates a random number between and including the given range.
  230.      * @param min is the minimum value the random number will be.
  231.      * @param max is the maximum value the random number will be.
  232.      * @return a random number between min and max inclusively.
  233.      * @exception IllegalArgumentException if min is not less than max.
  234.      */
  235.     public static int rangedRandom(int min, int max)
  236.     {
  237.         if (min > max)
  238.             throw new IllegalArgumentException("min (" + min + ") must be less than max (" + max + ").");
  239.         
  240.         int range = max - min;
  241.         double rand = Math.random();
  242.         return (int)((rand * range) + min + 0.5);
  243.     }
  244. }
  245.